package yxy.game.pm2.active.command.holiday;

import yxy.cherry.data.DataCenter;
import yxy.cherry.data.bean.atcholidayBOSS;
import yxy.cherry.game.base.message.MessageEmitterHandler;
import yxy.cherry.game.base.message.annotation.MessageSource;
import yxy.cherry.game.base.message.annotation.MessageType;
import yxy.game.pm2.Result;
import yxy.game.pm2.active.handle.Act_HolidayBossChallengeHandle;
import yxy.game.pm2.active.module.ActiveModule_节日活动;
import yxy.game.pm2.command.Command;
import yxy.game.pm2.message.ActiveMessage;

/**
 * Author HYB
 * 挑战年兽
 */
@MessageType(proto = Command.ActHolidayChallengeRequest, classtype = ActiveMessage.ActHolidayChallengeRequest.class)
public class CMD_Active_节日_挑战年兽 extends Command<ActiveMessage.ActHolidayChallengeRequest> {

    @Override
    public void execute(MessageSource messageSource, ActiveMessage.ActHolidayChallengeRequest request) throws Exception {
        String pcode = messageSource.getTag();
        ActiveMessage.ActHolidayChallengeResponse.Builder response = ActiveMessage.ActHolidayChallengeResponse.newBuilder();

        ActiveModule_节日活动 am = ActiveModule_节日活动.get(pcode,false);
        if (am == null) {
            send(messageSource, response, Result.ActNoOpen);
            return;
        }
        atcholidayBOSS boss = DataCenter.getData(1001, atcholidayBOSS.class);
        if (boss == null) {
            send(messageSource, response, Result.DataError);
            return;
        }
        int frequency = boss.getFrequency();
        int challenge = am.getChallenge();
        if (frequency - challenge < 0) {
            send(messageSource, response, Result.ArenaTimesLack);
            return;
        }
        am.addChallenge();
        Act_HolidayBossChallengeHandle handle = new Act_HolidayBossChallengeHandle();

        Result result = handle.battleResponseExecute(pcode, boss, response);
        if (result != Result.Success) {
            send(messageSource, response, Result.DataError);
        }

        send(messageSource, response, Result.Success);
    }

    private void send(MessageSource messageSource, ActiveMessage.ActHolidayChallengeResponse.Builder response, Result result) {
        response.setResult(result.packResult());
        MessageEmitterHandler.getInstance().send(messageSource, response.build());
    }
}
